package com.oazon.moco.playlist;

import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
import android.content.Context;
import android.util.Log;

import com.oazon.moco.bean.Metadata;
import com.oazon.moco.playlist.db.PlaylistDao;

public class MediaPlayList extends PlayList<Metadata>{
	private PlaylistDao mPlaylistDao;
	private String TAG = "MediaPlayList";
	private static MediaPlayList rList = null;
	
	public MediaPlayList(Context context) {
		mPlayList = new Vector<Metadata>();
		mPlaylistDao = new PlaylistDao(context);
	}
	
	public static MediaPlayList getInstance (Context context) {
		if (rList == null) {
			rList = new MediaPlayList(context);
			
			return rList;
		} 
		
		return rList;
	}
	
	@Override
	public synchronized boolean add(Metadata metaData){
		boolean result = false;
		try {
			result = mPlayList.add(metaData);
		} catch (Exception e) {
			e.printStackTrace();
		}
		dumpPlaylist();
		return result;
	}
	
	@Override
	public synchronized boolean addAll(List<Metadata> metadatas){
		
/*		if (metadatas == null || (metadatas.size() + size()) > maxSize) {
			for (int i = 0; i < metadatas.size(); i++) {
				removeFirst();
			}
		}
*/		
		boolean result = false;
		try {
			result = mPlayList.addAll(metadatas);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		dumpPlaylist();
		//syncDb(vector2List(mPlayList));
		return result;
	}
	
	@Override
	public synchronized boolean addAll(int index, List<Metadata> metadatas){
		if (index > mPlayList.size() || index < 0)
			return false;
		
		boolean result = false;
		try {
			result = mPlayList.addAll(index, metadatas);
		} catch (Exception e) {
			e.printStackTrace();
		}
		dumpPlaylist();
		//syncDb(vector2List(mPlayList));
		return result;
	}
	
	@Override
	public synchronized boolean add(int index, Metadata metaData){
		
		try {
		    mPlayList.add(index, metaData);
		    return true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
		//syncDb(vector2List(mPlayList));
	}
	
	@Override
	public synchronized Metadata remove(int location){
		if (location >= mPlayList.size() || location < 0) 
			return null;
		
		Metadata result = null;
		try {
			result = mPlayList.remove(location);
			android.util.Log.d(TAG, "remove " + result.getTitle() + " isRecommand:" + result.isRecommand());
		}catch (Exception e) {
			e.printStackTrace();
		}
		
		//syncDb(vector2List(mPlayList));
		return result;
	}
	
	@Override
	public Metadata removeFirst() {
		Metadata result = null;
		try {
			result = mPlayList.remove(0);
			Log.d(TAG, "remove " + result.getTitle() + " isRecommand:" + result.isRecommand());
		}catch (Exception e) {
			e.printStackTrace();
		}
		
		//dumpPlaylist();
		
		return result;
	}
	
	@Override
	public synchronized void clear(){
		mPlayList.clear();
		//syncDb(vector2List(mPlayList));
	}
	
	@Override
	public int indexOf(Metadata metaData) {
		return mPlayList.indexOf(metaData);
	}
	
	@Override
	public Vector<Metadata> getPlayList() {
		return mPlayList;
	}
	
	@Override
	public int size() {
		return mPlayList.size();
	}
	
	private List<Metadata> vector2List(Vector<Metadata> data) {
		List<Metadata> result = new ArrayList<Metadata>();
		for (Metadata md : data) {
			if (!md.isRecommand())
			   result.add(md);
		}
		return result;
	}
	
	public synchronized void syncDb(List<Metadata> data) {
		//mPlaylistDao.saveAllPlayList(vector2List(mPlayList));
		Thread thread = new Thread(new Runnable(){
			@Override
			public void run() {
				mPlaylistDao.saveAllPlayList(vector2List(mPlayList));
				dumpPlaylist();
			}
		});
		thread.start();
	}
	
	public synchronized void syncDb() {
		Log.d(TAG, "sync db");
		syncDb(mPlayList);
	}
	
	
	public void dumpPlaylist() {
		Log.d(TAG, "-----------------------");
		for (Metadata md : mPlayList) {
		    android.util.Log.d(TAG, md.getTitle() + " Recommand:" + md.isRecommand() + " url:" + md.getRes().getValue() + "  provider:" + md.getServiceID());
		}
	}
	
}
